package com.ycrlkj.android.modulescore.main.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.ToastUtils;
import com.lxj.xpopup.impl.LoadingPopupView;
import com.lxj.xpopup.interfaces.OnConfirmListener;
import com.ycrlkj.android.modulescore.main.R;
import com.ycrlkj.android.modulescore.main.apiservice.UserApiService;
import com.ycrlkj.android.modulescore.main.databinding.FragmentUserPasswordEditBinding;
import com.ycrlkj.android.modulescore.main.entity.User;
import com.ycrlkj.android.modulescore.main.viewmodel.UserViewModel;
import com.ycrlkj.android.modulespublic.common.constant.RoutePath;
import com.ycrlkj.android.modulespublic.common.net.exception.ApiException;
import com.ycrlkj.android.modulespublic.common.net.exception.ApiExceptionHandler;
import com.ycrlkj.android.modulespublic.common.net.exception.ErrorConsumer;
import com.ycrlkj.android.modulespublic.common.net.response.ResultData;
import com.ycrlkj.android.modulespublic.common.net.transformer.ResponseTransformer;
import com.ycrlkj.android.modulespublic.common.utils.xpopup.XPopupUtil;
import com.ycrlkj.android.modulespublic.common.widget.titlebar.MTitleBar;

import javax.inject.Inject;

import dagger.hilt.android.AndroidEntryPoint;
import io.reactivex.functions.Consumer;

@AndroidEntryPoint
public class UserPasswordEditFragment extends Fragment {

    private FragmentUserPasswordEditBinding binding;

    private UserCenterActivity activity;

    private UserViewModel viewModel;

    @Inject
    UserApiService userApiService;

    private EditText etPassword;
    private EditText etNewPassword;
    private EditText etNewPassword1;

    private User user;

    public UserPasswordEditFragment() {
    }

    public static UserPasswordEditFragment newInstance() {
        UserPasswordEditFragment fragment = new UserPasswordEditFragment();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        activity = (UserCenterActivity) getActivity();
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_user_password_edit, container, false);
        viewModel = new ViewModelProvider(activity, new ViewModelProvider.AndroidViewModelFactory(activity.getApplication())).get(UserViewModel.class);

        user = viewModel.getUser().getValue();
        if (user == null) {
            XPopupUtil.showModal(activity, "未找到用户, 请重新登录", new OnConfirmListener() {
                @Override
                public void onConfirm() {
                    ARouter.getInstance().build(RoutePath.PAGE_LOGIN).navigation();
                }
            });
        }

        etPassword = binding.etPassword;
        etNewPassword = binding.etNewPassword;
        etNewPassword1 = binding.etNewPassword1;

        initTitleBar();
        initBtn();

        return binding.getRoot();
    }

    public void initTitleBar() {
        MTitleBar titleBar = binding.mTitleBar;
        titleBar.setBtnEvents(new MTitleBar.BtnEvents() {
            @Override
            public void setBtnLeftOnClickListener() {
                activity.toUserInfo();
            }

            @Override
            public void setBtnRightOnClickListener() {
                //隐藏按钮, 无事件
            }
        });
    }

    public void initBtn() {
        binding.btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                save();
            }
        });
    }


    public void save() {
        String etPasswordText = etPassword.getText().toString();
        String etNewPasswordText = etNewPassword.getText().toString();
        String etNewPasswordText1 = etNewPassword1.getText().toString();

        if (TextUtils.isEmpty(etPasswordText)) {
            ToastUtils.showLong("原密码不能为空");
            return;
        }
        if (TextUtils.isEmpty(etNewPasswordText)) {
            ToastUtils.showLong("新密码不能为空");
            return;
        }
        if (TextUtils.isEmpty(etNewPasswordText1)) {
            ToastUtils.showLong("确认密码不能为空");
            return;
        }

        if(!TextUtils.equals(etNewPasswordText, etNewPasswordText1)){
            ToastUtils.showLong("两次输入新密码不一致");
            return;
        }

        if (user == null) {
            XPopupUtil.showModal(activity, "未找到用户, 请重新登录", new OnConfirmListener() {
                @Override
                public void onConfirm() {
                    ARouter.getInstance().build(RoutePath.PAGE_LOGIN).navigation();
                }
            });
        }

        String id = user.getId();

        User form = new User();

        form.setId(id);
        form.setPassword(etPasswordText);
        form.setNewPassword(etNewPasswordText);

        userEditPassword(form);
    }

    @SuppressLint("CheckResult")
    public void userEditPassword(User form) {
        LoadingPopupView loadingPopupView = XPopupUtil.showLoading(activity);
        XPopupUtil.hideLoadingDelay(loadingPopupView);
        userApiService.editPassword(form)
                .compose(ResponseTransformer.obtain())
                .subscribe(new Consumer<ResultData>() {
                    @Override
                    public void accept(ResultData resultData) throws Exception {
                        XPopupUtil.hideLoading(loadingPopupView);
                        ToastUtils.showLong(resultData.getToast());
                    }
                }, new ErrorConsumer() {
                    @Override
                    protected void error(ApiException e) {
                        XPopupUtil.hideLoading(loadingPopupView);
                        ApiExceptionHandler.doWith(activity, e);
                    }
                });
    }


}